import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Scanner;

// jdbc
public class Main {
    public static void main5(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/lyw?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");

        Connection connection = dataSource.getConnection();

        String sql = "select * from stu";

        PreparedStatement statement = connection.prepareStatement(sql);

        ResultSet set = statement.executeQuery();

        // 遍历结果集合
        while (set.next()) {
            // 将resultSet理解为一个表,初始情况下,表的光标位于表的第一行的上面
            int id = set.getInt("id");
            String name = set.getString("name");
            System.out.println(id + name);
        }
        statement.close();
        connection.close();

    }
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        int id = scanner.nextInt();
        String name = scanner.next();
        // 数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/lyw?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");
        // 连接
        Connection connection = dataSource.getConnection();
        // 构造sql语句
        String sql = "insert into stu values (? , ?)";
        // 预编译
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);
        // 执行
        statement.executeUpdate();
        // 关闭 最后断开连接
        statement.close();
        connection.close();
    }
    public static void main2(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        int id = scanner.nextInt();
        String name = scanner.next();
        // 1. 创建初始化一个数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/lyw?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");
        // 2.连接数据库
        Connection connection = dataSource.getConnection();
        // 3.构造sql语句
        String sql = "insert into stu values (? , ?)";
        // 4.预编译 + 进行数据替换
        PreparedStatement statement =  connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);
        System.out.println(statement);
        // 5.执行
        int ret = statement.executeUpdate();
        // 6. 关闭资源 -- 最后断开连接
        statement.close();
        connection.close();
    }
    //jdbc代码步骤
    public static void main1(String[] args) throws SQLException {
        //1.创建并初始化一个数据源 --- 要了一个美女的电话号码
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).
                setUrl("jdbc:mysql://127.0.0.1:3306/lyw?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");
        //2.和数据库服务器建立连接 --- 和美女打电话
        //TODO 接收connection时,需要的是java.sql包底下爱的,不能是jdbc的
        Connection connection = dataSource.getConnection(); // 美女接电话
        //3.构造sql语句
        String sql = "insert into stu value (1,'jim')";
        //4.对sql语句进行预编译
        PreparedStatement statement =
                connection.prepareStatement(sql);
        //5.执行sql语句 将预编译的sql语句发给数据库服务器
        // delete update insert 都是executeUpdate select是executeQuery
        int ret = statement.executeUpdate();

        //6.释放资源
        statement.close();
        connection.close();
    }
}
